﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using HZU.ZP.Domain.IService.Resume;
using HZU.ZP.Domain.IService;
using HZU.ZP.Domain.Model.Resume;
using HZU.ZP.Domain.Model;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace HZU.ZP.Controllers
{
    public class ResumeController : Controller
    {
        private IApplicantService _applicantService;
        private IAwardService _awardService;
        private IEducationExperienceService _educationExperienceService;
        private IInformationService _informationService;
        private IJobIntentionService _jobIntentionService;
        private IMasterSkillService _masterSkillService;
        private IProjectExperienceService _projectExperienceService;
        private ISelfEvaluationService _selfEvaluationService;

        public ResumeController(IAwardService awardService, IEducationExperienceService educationExperienceService,
            IInformationService informationService, IJobIntentionService jobIntentionService,
            IMasterSkillService masterSkillService, IProjectExperienceService projectExperienceService,
            ISelfEvaluationService selfEvaluationService, IApplicantService applicantService)
        {
            _awardService = awardService;
            _educationExperienceService = educationExperienceService;
            _informationService = informationService;
            _jobIntentionService = jobIntentionService;
            _masterSkillService = masterSkillService;
            _projectExperienceService = projectExperienceService;
            _selfEvaluationService = selfEvaluationService;
            _applicantService = applicantService;
        }
        // GET: /<controller>/
        public IActionResult Index()
        {
            return View();
        }

        [Authorize]
        [HttpGet]
        public IActionResult GetIdResume(int applicantId)
        {
            ResumeInfo resume = new ResumeInfo();
            resume.Awards = _awardService.GetAwardList(applicantId);
            resume.EducationExperiences = _educationExperienceService.GetEducationExperienceList(applicantId);
            resume.Information = _informationService.GetInformation(applicantId);
            resume.JobIntention = _jobIntentionService.GetJobIntention(applicantId);
            resume.MasterSkills = _masterSkillService.GetMasterSkillList(applicantId);
            resume.ProjectExperiences = _projectExperienceService.GetProjectExperienceList(applicantId);
            resume.SelfEvaluation = _selfEvaluationService.GetSelfEvaluation(applicantId);

            //1：已创建简历，0：未创建简历
            if (resume.Information == null && resume.SelfEvaluation == null && resume.JobIntention == null && resume.MasterSkills.Count() == 0 && resume.Awards.Count() == 0 && resume.ProjectExperiences.Count() == 0 && resume.EducationExperiences.Count() == 0)
                ViewData["ReturnCode"] = 0;
            else
                ViewData["ReturnCode"] = 1;

            ViewData["Title"] = "学生简历";
            return View(resume);
        }

        [Authorize(Roles = "Applicant")]
        [HttpGet]
        // GET: /<controller>/
        public IActionResult GetResume(string userEmail)
        {
            ResumeInfo resume = new ResumeInfo();
            var applicant = _applicantService.GetApplicant(userEmail);
            if (applicant == null)
                return NotFound();
            int applicantId = applicant.Id.Value;
            ViewData["applicantId"] = applicantId;
            resume.Awards = _awardService.GetAwardList(applicantId);
            resume.EducationExperiences = _educationExperienceService.GetEducationExperienceList(applicantId);
            resume.Information = _informationService.GetInformation(applicantId);
            resume.JobIntention = _jobIntentionService.GetJobIntention(applicantId);
            resume.MasterSkills = _masterSkillService.GetMasterSkillList(applicantId);
            resume.ProjectExperiences = _projectExperienceService.GetProjectExperienceList(applicantId);
            resume.SelfEvaluation = _selfEvaluationService.GetSelfEvaluation(applicantId);

            ViewData["Title"] = "我的简历";
            return View(resume);
        }

        [Authorize(Roles = "Applicant")]
        [HttpPost]
        public IActionResult AddInformation([FromForm] Information information)
        {
            _informationService.AddInformation(information);
            return Json(AjaxResult.ReturnMessage(1, "添加成功"));
        }

        [Authorize(Roles = "Applicant")]
        [HttpPost]
        public IActionResult EditInformation([FromForm]Information information)
        {
            _informationService.UpdateInformation(information);
            return Json(AjaxResult.ReturnMessage(1, "修改成功"));
        }

        [Authorize(Roles = "Applicant")]
        [HttpPost]
        public IActionResult DeletInformation(int id)
        {
            _informationService.DeleteInformation(id);
            return Json(AjaxResult.ReturnMessage(1, "删除成功"));
        }

        [Authorize(Roles = "Applicant")]
        [HttpPost]
        public IActionResult AddSelfEvaluation([FromForm] SelfEvaluation selfEvaluation)
        {
            _selfEvaluationService.AddSelfEvaluation(selfEvaluation);
            return Json(AjaxResult.ReturnMessage(1, "添加成功"));
        }

        [Authorize(Roles = "Applicant")]
        [HttpPost]
        public IActionResult EditSelfEvaluation([FromForm]SelfEvaluation selfEvaluation)
        {
            _selfEvaluationService.UpdateSelfEvaluation(selfEvaluation);
            return Json(AjaxResult.ReturnMessage(1, "修改成功"));
        }

        [Authorize(Roles = "Applicant")]
        [HttpPost]
        public IActionResult DeletSelfEvaluation(int id)
        {
            _selfEvaluationService.DeleteSelfEvaluation(id);
            return Json(AjaxResult.ReturnMessage(1, "删除成功"));
        }

        [Authorize(Roles = "Applicant")]
        [HttpPost]
        public IActionResult AddJobIntention([FromForm] JobIntention jobIntention)
        {
            _jobIntentionService.AddJobIntention(jobIntention);
            return Json(AjaxResult.ReturnMessage(1, "添加成功"));
        }

        [Authorize(Roles = "Applicant")]
        [HttpPost]
        public IActionResult EditJobIntention([FromForm]JobIntention jobIntention)
        {
            _jobIntentionService.UpdataJobIntention(jobIntention);
            return Json(AjaxResult.ReturnMessage(1, "修改成功"));
        }

        [Authorize(Roles = "Applicant")]
        [HttpPost]
        public IActionResult DeletJobIntention(int id)
        {
            _jobIntentionService.DeleteJobIntention(id);
            return Json(AjaxResult.ReturnMessage(1, "删除成功"));
        }

        [Authorize(Roles = "Applicant")]
        [HttpPost]
        public IActionResult AddMasterSkill([FromForm] MasterSkill masterSkill)
        {
            _masterSkillService.AddMasterSkill(masterSkill);
            return Json(AjaxResult.ReturnMessage(1, "添加成功"));
        }

        [Authorize(Roles = "Applicant")]
        [HttpPost]
        public IActionResult EditMasterSkill([FromForm]MasterSkill masterSkill)
        {
            _masterSkillService.UpdateMasterSkill(masterSkill);
            return Json(AjaxResult.ReturnMessage(1, "修改成功"));
        }

        [Authorize(Roles = "Applicant")]
        [HttpPost]
        public IActionResult DeleteMasterSkill(int id)
        {
            _masterSkillService.DeleteMasterSkill(id);
            return Json(AjaxResult.ReturnMessage(1, "删除成功"));
        }

        [Authorize(Roles = "Applicant")]
        [HttpPost]
        public IActionResult AddAward([FromForm] Award award)
        {
            _awardService.AddAward(award);
            return Json(AjaxResult.ReturnMessage(1, "添加成功"));
        }

        [Authorize(Roles = "Applicant")]
        [HttpPost]
        public IActionResult EditAward([FromForm]Award award)
        {
            _awardService.UpdateAward(award);
            return Json(AjaxResult.ReturnMessage(1, "修改成功"));
        }

        [Authorize(Roles = "Applicant")]
        [HttpPost]
        public IActionResult DeleteAward(int id)
        {
            _awardService.DeleteAward(id);
            return Json(AjaxResult.ReturnMessage(1, "删除成功"));
        }

        [Authorize(Roles = "Applicant")]
        [HttpPost]
        public IActionResult AddProjectExperience([FromForm] ProjectExperience projectExperience)
        {
            _projectExperienceService.AddProjectExperience(projectExperience);
            return Json(AjaxResult.ReturnMessage(1, "添加成功"));
        }

        [Authorize(Roles = "Applicant")]
        [HttpPost]
        public IActionResult EditProjectExperience([FromForm]ProjectExperience projectExperience)
        {
            _projectExperienceService.UpdateProjectExperience(projectExperience);
            return Json(AjaxResult.ReturnMessage(1, "修改成功"));
        }

        [Authorize(Roles = "Applicant")]
        [HttpPost]
        public IActionResult DeleteProjectExperience(int id)
        {
            _projectExperienceService.DeleteProjectExperience(id);
            return Json(AjaxResult.ReturnMessage(1, "删除成功"));
        }

        [Authorize(Roles = "Applicant")]
        [HttpPost]
        public IActionResult AddEducationExperience([FromForm] EducationExperience educationExperience)
        {
            _educationExperienceService.AddEducationExperience(educationExperience);
            return Json(AjaxResult.ReturnMessage(1, "添加成功"));
        }

        [Authorize(Roles = "Applicant")]
        [HttpPost]
        public IActionResult EditEducationExperience([FromForm]EducationExperience educationExperience)
        {
            _educationExperienceService.UpdateEducationExperience(educationExperience);
            return Json(AjaxResult.ReturnMessage(1, "修改成功"));
        }

        [Authorize(Roles = "Applicant")]
        [HttpPost]
        public IActionResult DeleteEducationExperience(int id)
        {
            _educationExperienceService.DeleteEducationExperience(id);
            return Json(AjaxResult.ReturnMessage(1, "删除成功"));
        }
    }
}
